package org.picommo.common;

import org.simpleframework.xml.Attribute;

public class Vec2 {
	
	@Attribute
	final int x;
	@Attribute
	final int y;

	public int getX() { return x; }
	public int getY() { return y; }
	
	
	public Vec2() {
		this(0, 0);
	}
	
	public Vec2(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public static Vec2 getZero() {
		return new Vec2(0,0);
	}
	
	public boolean isZero() {
		return x == 0 && y == 0;
	}
	
	public Vec2 getLeftNormal() throws IllegalArgumentException {
		if(isZero()) {
			throw new IllegalArgumentException("Can't get normal for zero vector");
		}
		return new Vec2(-y, x);
	}
	
	public Vec2 getRightNormal() throws IllegalArgumentException {
		if(isZero()) {
			throw new IllegalArgumentException("Can't get normal for zero vector");
		}
		return new Vec2(y, -x);
	}
	
	public Vec2 getNegative() {
		return new Vec2(-x, -y);
	}
	
	
	public Vec2 add(Vec2 v) {
		return new Vec2(this.x + v.x, this.y + v.y);
	}
	
	public double getLength() {
		return Math.sqrt(x*x + y*y);
	}
	
	public double getLenghtSquared() {
		return x*x + y*y;
	}
	
	public boolean isWithin(Vec2 min, Vec2 max) {
		
		return x >= min.x && x <= max.x && y >= min.y && y <= max.y;
		
	}
	
	@Override
	public String toString() {
		return "(" + x + ", " + y + ")";
	}
	
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof Vec2)) {
			return false;
		}
		Vec2 v = (Vec2)obj;
		return x == v.x && y == v.y;
	}

}
